<?php

namespace sample\controller\fb;

use pmvc\mvc\security\SecurityUtil;
use pmvc\mvc\Controller;
use pmvc\mvc\Request;
use pmvc\mvc\Response;

class AuthorizeAppController
	implements Controller {

	private $facebookAppId;
	private $facebookAppSecret;
	private $facebookAuthorizePath;
	private $notAuthorizedViewName;
	private $authorizedViewName;

	public function handleRequest(Request $request, Response $response) {
		if ($request->hasParameter("error")) {
			return $this->notAuthorizedViewName;

		// it's a simple authorization
		} else if ($request->hasParameter("code")) {

			// get access key and expiration
			$contents = file_get_contents(
				"https://graph.facebook.com/oauth/access_token"
    			."?client_id=".urlencode($this->facebookAppId)
     			."&client_secret=".urlencode($this->facebookAppSecret)
    			."&redirect_uri=".urlencode($request->getBaseUrl().$this->facebookAuthorizePath)
     			."&code=".urlencode($request->getParameter("code")));
     		if ($contents===FALSE) {
     			throw new Exception("Error getting access key from facebook: ".$contents);
     		}
		}
     		
		return $this->authorizedViewName;
	}

	public function setFacebookAppId($facebookAppId) {
		$this->facebookAppId = $facebookAppId;
	}

	public function setFacebookAppSecret($facebookAppSecret) {
		$this->facebookAppSecret = $facebookAppSecret;
	}

	public function setFacebookAuthorizePath($facebookAuthorizePath) {
		$this->facebookAuthorizePath = $facebookAuthorizePath;
	}
	
	public function setNotAuthorizedViewName($notAuthorizedViewName) {
		$this->notAuthorizedViewName = $notAuthorizedViewName;
	}
	
	public function setAuthorizedViewName($authorizedViewName) {
		$this->authorizedViewName = $authorizedViewName;
	}
	
}

?>